<?php
namespace App\Http\Controllers\AdminApi;
use App\Model\ServiceTable;
use App\Repository\Admin\AdminService;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
//服务管理
class ServiceController extends ApiController{
	//服务-列表
    public function getServiceList(){
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);
        $title = \Request::get('title');
        $offset = ($page - 1) * $limit;
        //拼接sql
        $where = "1=1 and d.status>0";
        $parameters = array(
            'limit' => $limit,
            'offset' => $offset
        );
        if ($title) {
            $where .= " AND  d.title LIKE :title ";
            $parameters['title'] = $title;
        }
        $sql = "select * from service as d  WHERE {$where} ORDER BY d.id DESC limit  :offset,:limit;";
        //执行sql
        $res = \DB::select($sql, $parameters);
        unset($parameters['limit'], $parameters['offset']);
        $count = \DB::select("select count(*) as c from service as d WHERE {$where}", $parameters)[0]->c;
        $data=[];
        foreach ($res as $v) {
        	$v->type_text=ConstTable::SERVICE_TYPE[$v->type];
        	$data[] = $v;
        }
        return JsonRespose::success([
            'count' => $count,
            'limit' => $limit,
            'page' => $page,
            'res' => $data
        ]);
    }
    //服务-详情
    public function getServiceInfo(){
        $id = \Request::get('id');
        return JsonRespose::done(ServiceTable::getServiceInfo($id));
    }
    //服务-添加修改
    public function postServiceSave(){
        //获取所有参数到$param数组
        $param = \Request::all();
        $data = [
            'id' => $param['id'],
            'title' => $param['title'],
            'img_url' => $param['img_url'],
            'content' => $param['content'],
        ];
        //var_dump($param['content']);
        //判断是否存在id
        $res = ServiceTable::ORM()->where('id', $param['id'])->update($data);
        if ($res) {
            //接口返回数据
            return JsonRespose::success(['id' => $param['id']]);
        } else {
            return JsonRespose::fail();
        }
    }
}


